﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Model;
using QXBook.Utils;
using QXBook.Service;

namespace Web.Shopping
{
    public partial class ShoppingCart : UserBasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!IsPostBack)
            {
                TextBind();
            }

            imgbtnSure.Click += new ImageClickEventHandler(imgbtnSure_Click);
            imgbtnNext.Click += new ImageClickEventHandler(imgbtnNext_Click);
		}

        void imgbtnSure_Click(object sender, ImageClickEventArgs e)
        {
            foreach (RepeaterItem item in rptList.Items)
            {
                HiddenField hddID = (HiddenField)item.FindControl("hddID");
                TextBox txtCount = (TextBox)item.FindControl("txtCount");

                ServiceFactory.CreateInstance<ShoppingCartService>().UpdateShoppingCartCount(CommonUtils.ToInt(hddID.Value), CommonUtils.ToInt(txtCount.Text));
            }

            OrderHeaderInfo orderHeader = new OrderHeaderInfo();
            orderHeader.Consignee = iConsignee.Value;
            orderHeader.Telphone = iTelphone.Value;
            orderHeader.Address = iAddress.Value;
            orderHeader.UserID = this.userInfo.UserID;

            ServiceFactory.CreateInstance<ShoppingCartService>().CreateOrder(this.userInfo, orderHeader);
            Response.Redirect("Payroll.aspx");
        }

        void imgbtnNext_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/Product/Product_List.aspx");
        }

        private void TextBind()
        {
            List<ShoppingCartInfo> list = ServiceFactory.CreateInstance<ShoppingCartService>().GetShoppingCartByToken(null);

            this.rptList.DataSource = list;
            this.rptList.DataBind();

            decimal money = 0;
            foreach (ShoppingCartInfo info in list)
            {
                money += info.Price;
            }
            litMoney.Text = money.ToString("0.00");

            if (this.userInfo != null)
            {
                phPayInfo.Visible = true;
                phNoLogin.Visible = false;
            }
            else
            {
                phPayInfo.Visible = false;
                phNoLogin.Visible = true;
            }
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ShoppingCartInfo cart = e.Item.DataItem as ShoppingCartInfo;
            Label lblTitle = (Label)e.Item.FindControl("lblTitle");
            HiddenField hddID = (HiddenField)e.Item.FindControl("hddID");
            TextBox txtCount = (TextBox)e.Item.FindControl("txtCount");
            Literal litMoney = (Literal)e.Item.FindControl("litMoney");

            lblTitle.Text = cart.ProductName;
            hddID.Value = cart.CartID.ToString();
            txtCount.Text = cart.Count.ToString();
            litMoney.Text = cart.Price.ToString("0.00");
        }

        protected void rptList_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            Image imgJian = (Image)e.Item.FindControl("imgJian");
            TextBox txtCount = (TextBox)e.Item.FindControl("txtCount");
            Image imgJia = (Image)e.Item.FindControl("imgJia");
            Literal litMoney = (Literal)e.Item.FindControl("litMoney");

            if (imgJian != null && txtCount != null && imgJia != null && litMoney != null)
            {
                decimal money = CommonUtils.ToDecimal(litMoney.Text);
                imgJian.Attributes.Add("onclick", "editcount('" + txtCount.Text + "',-" + money + ",-1)");
                imgJia.Attributes.Add("onclick", "editcount('" + txtCount.Text + "'," + money + ",1)");
                //imgJian.Attributes.Add("onclick", "editcount('<%=txtCount.ClientID %>',-<%# Eval("Price") %>,-1)")
            }
        }
	}
}